import down from "../../../../imgs/down.jpg";
import CommonUtils from "../../common/utils/CommonUtils";
import EventEmitterUtils from "../../common/utils/EventEmitterUtils";
import Icon from "@ui/RJMart/icons";
import inputNumber from "@ui/RJMart/inputNumber";

export default class AddNumber extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            num: this.props.defaultAmount || 1
        };
    }

    setCount() {
        const { num } = this.state;
        this.props.setCount(num);
    }

    emitCount() {
        setTimeout(() => {
            EventEmitterUtils.emit("cartChoosedTotalCountChange", null);
        }, 100);
    }

    render() {
        const { num } = this.state;
        const { selected } = this.props;

        return (
            <div className={"cart-addnum"}>
                <input
                    className={
                        selected
                            ? "cart-pro-count-input-value cart-pro-count-input-value-normal"
                            : "cart-pro-count-input-value-normal"
                    }
                    type="text"
                    value={num}
                    onChange={e => {
                        let txt = e.target.value;
                        if (txt == "") {
                            this.setState({
                                num: e.target.value
                            });
                        }
                        if (!CommonUtils.isNum(txt)) {
                            return;
                        }
                        this.setState(
                            {
                                num: e.target.value - 0
                            },
                            this.setCount
                        );
                        this.emitCount();
                    }}
                />
                <div>
                    <div
                        onClick={() => {
                            this.setState(
                                {
                                    num: num - 0 + 1
                                },
                                this.setCount
                            );
                            this.emitCount();
                        }}
                        className={"cart-addnum-add"}
                    >
                        <img src={down} alt="" />
                    </div>
                    <div
                        onClick={() => {
                            if (num == 1) {
                                return;
                            }
                            this.setState(
                                {
                                    num: num - 1
                                },
                                this.setCount
                            );
                            this.emitCount();
                        }}
                        className={"cart-addnum-jian"}
                    >
                        <img src={down} alt="" />
                    </div>
                </div>
            </div>
        );
    }
}
